package com.ruoyi.framework.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

/**
 * redisson 配置类
 *
 * @author siashan
 * @date 2023/4/4
 */
@Configuration
public class RedissonConfig {

    @Autowired
    private Environment env;

    @Bean
    public RedissonClient redisson() throws IOException {
        // 本例子使用的是yaml格式的配置文件，读取使用Config.fromYAML，如果是Json文件，则使用Config.fromJSON
        String[] activeProfiles = env.getActiveProfiles();
        System.out.println(activeProfiles.length);
        String profile = "";
        if (activeProfiles.length > 0) {
            profile = "-" + activeProfiles[0];
        }
        String yml = "redisson-config" + profile + ".yml";
        Config config = Config.fromYAML(new ClassPathResource(yml).getInputStream());
        return Redisson.create(config);
    }

}
